/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：RouterMessageHandlerImpl.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.cneport.transfer.message.dxp.DxpCtrl;
import com.cneport.transfer.router.model.Router;
import com.cneport.transfer.router.support.service.RouterService;
import com.cneport.transfer.util.DateUtil;
import com.cneport.transfer.util.XmlSerializer;

public class DxpRouterMessageHandlerImpl implements RouterMessageHandler {

    @Autowired
    private RouterService routerInfoService;
    
    @Transactional
    public String routerMessageControl(String routerMessage,String routerType) {
        Object ctlMessage = null;
        try {
            ctlMessage = XmlSerializer
                    .deSerialize(routerMessage, DxpCtrl.class);

        } catch (Exception e) {

        }
        List<Router> routerInfoList=object2Model(ctlMessage,routerType);
        routerInfoService.deleteRouter(routerType);
        routerInfoService.insertRouter(routerInfoList);
        return null;
    }

    public List<Router> object2Model(Object routerMessage,String routerType) {
        DxpCtrl dxpCtrl = (DxpCtrl) routerMessage;
        List<Router> routerInfoList = null;
        List<DxpCtrl.RouteList.RouteInfo> dxpRouterInfoList = dxpCtrl
                .getRouteList().getRouteInfo();
        Date routerEffectTime = DateUtil.convert2Date(dxpCtrl.getRouteList()
                .getEfficientTime());
        String routerVer = dxpCtrl.getRouteList().getVersion();
        String startNode = dxpCtrl.getRecNodeId();
        for (DxpCtrl.RouteList.RouteInfo dxpRouterInfo : dxpRouterInfoList) {
            Router routerInfo = new Router();
            routerInfo.setEndNode(dxpRouterInfo.getEndNode());
            routerInfo.setRouterEffectTime(routerEffectTime);
            routerInfo.setRouterExpression(dxpRouterInfo.getNextNode());
            routerInfo.setRouterVer(routerVer);
            routerInfo.setStartNode(startNode);
            routerInfo.setRouterType(routerType);
            routerInfoList.add(routerInfo);
        }
        return routerInfoList;
    }
}
